#! /bin/bash

if  [ -n "$1" ]; then
  data_date="$1"
else
  data_date=$(date -d  "-1 days " +%F )
fi


Load_Data_TO_DIM_SQL="
with t0 as (
    select id,AREANAME province from jtp_app_ods.ods_md_areas where PARENTID = 0
),t1 as (
    select t2.id,t0.province,t2.AREANAME city from t0 left join jtp_app_ods.ods_md_areas t2 on t0.ID =  t2.PARENTID
),t3 as (
    select t4.id,t1.province,t1.city,t4.AREANAME district from t1 left join jtp_app_ods.ods_md_areas t4 on t4.PARENTID = t1.ID
)
insert into  table jtp_app_dim.dim_md_areas partition (dt='$data_date')
    select t6.id,t3.province,t3.city,t3.district,t6.AREANAME street,t6.BD09_LNG,t6.BD09_LAT
         ,GeoHash(BD09_LNG,BD09_LAT) geo_hash
    from t3 left join jtp_app_ods.ods_md_areas t6 on t3.ID = t6.PARENTID where BD09_LAT is not null and BD09_LNG is not null
    and t6.dt = '$data_date';"

/opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${Load_Data_TO_DIM_SQL}"